package com.example.demo.service.impl;

import com.example.demo.entity.Safetyinspectionstatistics;
import com.example.demo.handle.StatisticsListParseHandler;
import com.example.demo.mapper.SafetyinspectionstatisticsMapper;
import com.example.demo.service.SafetyinspectionstatisticsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hys
 * @since 2021-07-19
 */
@Service
public class SafetyinspectionstatisticsServiceImpl extends ServiceImpl<SafetyinspectionstatisticsMapper, Safetyinspectionstatistics> implements SafetyinspectionstatisticsService {

    @Autowired
    private SafetyinspectionstatisticsMapper safetyinspectionstatisticsMapper;

    @Override
    public Integer insertStatisticsData(String statisticsXml) {
        //1.获取一个SAXParserFactory的实例对象
        SAXParserFactory factory = SAXParserFactory.newInstance();
        //2.通过factory的newSAXParser()方法获取一个SAXParser类的对象。
        try {
            SAXParser parser = null;
            try {
                parser = factory.newSAXParser();
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
            } catch (SAXException e) {
                e.printStackTrace();
            }
            //创建SAXParserHandler对象
            StatisticsListParseHandler statisticsListParseHandler = new StatisticsListParseHandler();
            try {
                //读取井口信息文件
                parser.parse(statisticsXml, statisticsListParseHandler);

            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("~~~~~共有" + statisticsListParseHandler.getSafetyinspectionstatisticsArrayList().size() + "个信息");
            // 根据minecode去查找数据库
            for (Safetyinspectionstatistics safetyinspectionstatistics : statisticsListParseHandler.getSafetyinspectionstatisticsArrayList()) {
                System.out.println(safetyinspectionstatistics.getCsMineCode());
                System.out.println(safetyinspectionstatistics.getCsDataTime());
                System.out.println(safetyinspectionstatistics.getSsStationCode());
                System.out.println(safetyinspectionstatistics.getSsTransducerCode());
                System.out.println(safetyinspectionstatistics.getSsAnalogAvgvalue());
                System.out.println(safetyinspectionstatistics.getSsAnalogMaxvalue());
                System.out.println(safetyinspectionstatistics.getSsAnalogMaxtime());
                System.out.println(safetyinspectionstatistics.getSsAnalogMinvalue());
                System.out.println(safetyinspectionstatistics.getSsAnalogMintime());

                System.out.println("----------------------------安全监控信息finish---------------------------------");

                safetyinspectionstatisticsMapper.insertStatisticsData(safetyinspectionstatistics.getCsMineCode(),safetyinspectionstatistics.getCsDataTime(),safetyinspectionstatistics.getSsStationCode(),safetyinspectionstatistics.getSsTransducerCode(),safetyinspectionstatistics.getSsAnalogAvgvalue(),safetyinspectionstatistics.getSsAnalogMaxvalue(),safetyinspectionstatistics.getSsAnalogMaxtime(),safetyinspectionstatistics.getSsAnalogMinvalue(),safetyinspectionstatistics.getSsAnalogMintime());
            }


        } catch (SAXException e) {
            e.printStackTrace();
        }
        return 1;
    }
}
